<?php
require_once ('../lib/config.php');
require_once ('../lib/facebook.php');
require_once ('../lib/friends.php');
//require_once ('../lib/friend.php');
require_once ('../lib/test.php');
require_once ('../../simpletest/autorun.php');
require_once ('../lib/init.php');

class TestFunc_callFQL extends UnitTestCase {
    
    function setUp(){
        global $login_button,$facebook;
        $user = $facebook->getUser();
        if(!$user){
            try{
                $me = getOneself();
            }catch(Exception $e){
                print($login_button);
                exit();
            }
        }
    }
    
    //refer to http://www.simpletest.org/en/unit_test_documentation.html
//    function testCallFQL(){ 
//    
//        $id = 612139051;
//        $messageQuery = "SELECT thread_id, author_id, body, created_time FROM message WHERE author_id = '$id' AND thread_id IN (
//                    	SELECT thread_id FROM thread WHERE (folder_id = 0 OR folder_id = 1) AND updated_time > ".QUERY_SINCE."
//                	) AND created_time > ".QUERY_SINCE;
//            
//        $response = callFQL($messageQuery);
//        
//        $this->assertNotNull($response);
//        $this->assertTrue(is_array($response));
//        
//        $query = null;
//        $response = callFQL($query);
//        $this->assertNull($response);
//        
//        $query = "";
//        $response = callFQL($query);
//        $this->assertNull($response);
//        
//        $query = "select 123";
//        try{
//            $response = callFQL($query);
//            $this->assertTrue(false);
//        }catch(Exception $e){
//            $this->assertTrue(true);
//        }
//        
// 
//    }
    
    function testQuery1(){
        $now = time();      

        $query = "SELECT thread_id, recipients, updated_time FROM thread 
        				WHERE (folder_id = 0 OR folder_id = 1) AND updated_time > ".QUERY_SINCE;
        

        try{
            
            $response = callFQL($query);

            error_log("success : ".(time() - $now));
          
            $this->assertTrue(true);
            
            foreach($response as $thread){
                $this->assertTrue($thread['updated_time'] > QUERY_SINCE, $thread['updated_time']." < ".QUERY_SINCE." in ".$thread['thread_id']);
            }
            
            
        }catch(Exception $e){
            error_log("fail : ".(time() - $now));
            error_log($e);
            $this->assertTrue(false);
        }
        
    }
    
    function testQuery2(){
        $now = time();      
        
        $query = "SELECT thread_id, author_id, created_time FROM message 
        				WHERE thread_id IN (
                        	SELECT thread_id FROM #threadQuery
                    	) AND created_time > ".QUERY_SINCE;
        

        try{
            
//            $response = callFQL($query);
            
            $this->assertTrue(true);
            
        }catch(Exception $e){
            error_log("fail : ".(time() - $now));
            error_log($e);
            $this->assertTrue(false);
        }
        
    }
}
?>
